/*
    XTU OJ: 1462
    by: fifth_light
*/

#include <stdio.h>

char str_1[] = "ji";
char str_2[] = "zhiyin";

int main(void) {
    int n, m;
    scanf("%d%d ", &n, &m);
    int ranges[10050];
    for (int i = 0; i < n; i++) {
        int cnt = 0, c, pos = -1, cond = -1, l1, l2;
        char str[100];
        scanf(" %n%s%n ", &l1, str, &l2);
        int l = l2 - l1;
        for (int i = 0; i < l; i++) {
            c = str[i];
            if (cond == -1) {
                if (c == 'j') {
                    cond = 1;
                    pos = 1;
                } else if (c == 'z') {
                    cond = 2;
                    pos = 1;
                } else {
                    cond = 3;
                    pos = -1;
                }
            } else if (cond == 1) {
                if (str_1[pos] == '\0') {
                    cnt++;
                    cond = 3;
                    pos = -1;
                } else if (c == str_1[pos]) {
                    pos++;
                } else {
                    cond = 3;
                    pos = -1;
                }
            } else if (cond == 2) {
                if (str_2[pos] == '\0') {
                    cnt++;
                    cond = 3;
                    pos = -1;
                } else if (c == str_2[pos]) {
                    pos++;
                } else {
                    cond = 3;
                    pos = -1;
                }
            }
            if (cond == 3) {
                if (c == 'j') {
                    cond = 1;
                    pos = 1;
                } else if (c == 'z') {
                    cond = 2;
                    pos = 1;
                } else {
                    cond = -1;
                }
            }
        }
        if ((cond == 1 && pos == 2) || (cond == 2 && pos == 6)) {
            cnt++;
        }
        ranges[i] = (i == 0 ? 0 : ranges[i - 1]) + cnt;
    }
    for (int i = 0; i < m; i++) {
        int l, r;
        scanf(" %d%d", &l, &r);
        if (l == 1) {
            printf("%d\n", ranges[r - 1]);
        } else {
            printf("%d\n", ranges[r - 1] - ranges[l - 2]);
        }
    }
    return 0;
}
